package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.exception.LoginException;
import cn.wolfcode.mapper.EmployeeMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IEmployeeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@Service
@Transactional
public class IEmployeeServiceImpl implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;

    public void setEmployeeMapper(EmployeeMapper employeeMapper) {
        this.employeeMapper = employeeMapper;
    }

    @Override
    public void delete(Long id) {
        employeeMapper.deleteByPrimaryKey(id);
    }

    @Override
    @Transactional
    public void save(Employee employee, Long[] ids) {
        employeeMapper.insert(employee);
        //处理中间表
    if (ids!=null && ids.length>0){
        employeeMapper.insertRelation(employee.getId(),ids);
         }

    }

    @Override
    public void update(Employee employee, Long[] ids) {
        employeeMapper.updateByPrimaryKey(employee);
        //删除关系
        employeeMapper.deleteRelation(employee.getId());
        //关联关系
        //处理中间表(关联关系)
        if (ids!=null && ids.length>0){
            employeeMapper.insertRelation(employee.getId(),ids);
        }

    }


    @Override
    public Employee get(Long id) {
        return employeeMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Employee> listall() {

        return employeeMapper.selectAll();
    }

    @Override
    public PageInfo<Employee> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());//里面两个参数开启分页,传入当前页和每一页的数量
        //就等于limit嘛,得限制性上面的startPage分页在执行下面的查询
        //得先分页在查询
        List<Employee> employees = employeeMapper.selectForList(qo);
        return new PageInfo<>(employees);
    }

    @Override
    public Employee login(String username, String password) {
        if (!StringUtils.hasText(username)) {
                throw new LoginException("用户名不能为空");
        }
        if (!StringUtils.hasText(password)) {
            throw new LoginException("密码不能为空");
        }
        Employee employee=employeeMapper.selectUserNameAndPassword(username,password);
        if(employee ==null){
            throw new LoginException("用户名或者密码错误");
        }
        Employee employee1 = new Employee();
        if (!employee.isStatus()){
            throw new LoginException("改账号已被禁用");
        }
        return employee;
    }

    @Override
    public String checkPassword(Long id,String oldPassword) {
        String password = employeeMapper.checkPasswordById(id);
        if(!oldPassword.equals(password)){
              throw new RuntimeException("滚你吗的");
        }
        return password;
    }

    @Override
    public List<String> selectByRoleId(Long id) {

        return employeeMapper.selectByRoleId(id);
    }

    @Override
    public Employee selectByUserName(String username) {

        return employeeMapper.selectByUserName(username);
    }

    @Override
    public Workbook exprtXls() {
        //文件下载的响应头（让浏览器访问资源的的时候以下载的方式打开）
        List<Employee> employees = employeeMapper.selectAll();
        //创建工作簿
        Workbook work = new HSSFWorkbook();
        //创建表
        Sheet sheet = work.createSheet("通讯率");
        //创建航
        Row row = sheet.createRow(0);
        //创建列
        row.createCell(0).setCellValue("用户名");
        row.createCell(1).setCellValue("姓名");
        row.createCell(2).setCellValue("邮箱");
        row.createCell(3).setCellValue("年龄");
        for (int i=0;i<employees.size();i++){
            //这里面也得减行列因为上面是给标题
             row = sheet.createRow(i + 1);
             //拿到每一个id所对应的员工
            Employee employee = employees.get(i);
            row.createCell(0).setCellValue(employee.getUsername());
            row.createCell(1).setCellValue(employee.getName());
            row.createCell(2).setCellValue(employee.getEmail());
            row.createCell(3).setCellValue(employee.getAge());
        }
        return work;
    }

    @Override
    public void importExxls(MultipartFile file) throws IOException {
        Workbook workbook = new HSSFWorkbook(file.getInputStream());
        //拿到他的标题列
        Sheet sheet = workbook.getSheetAt(0);
        //拿到他的最后一行然后在进行遍历
        int lastRowNum = sheet.getLastRowNum();
        //开始遍历
        for (int i=1;i<=lastRowNum;i++){
            Row row = sheet.getRow(i);
            Employee employee = new Employee();
            employee.setUsername(row.getCell(0).getStringCellValue());
            employee.setName(row.getCell(1).getStringCellValue());
            employee.setEmail(row.getCell(2).getStringCellValue());
            employee.setAge((int) row.getCell(3).getNumericCellValue());
            employeeMapper.insert(employee);
        }
    }

    @Override
    public void updatepwd(Long id, String newPassword) {
            employeeMapper.updatepwd(id,newPassword);
    }

    @Override
    public boolean selectBystatusId(Long id) {

        return employeeMapper.selectStatusId(id);
    }

    @Override
    public void updatestatus(Long id,boolean b) {
        Employee employee = new Employee();
        if(employee.getId()==id){
            throw  new RuntimeException("不能禁用自己的账号");
        }
        employeeMapper.updateStatus(id,b);
    }

    @Override
    public void resetpassword(Long id) {
        employeeMapper.resetpassword(id);
    }

    @Override
    public void deleteAll(Long[] ids) {
        employeeMapper.deleteAll(ids);
    }

}
